package com.example.shopdemo.utils;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ：Anitaxiaofeng
 * @description：图片处理Thumbnailator工具
 * @create 2018-01-17
 **/
public class ImageUtil {
//    public static void main(String[] args) throws IOException {
//        String basePath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
//        System.out.println(basePath);
//        Thumbnails.of(new File("/Users/xuwanying/Pictures/test/20140211224326_FietE.jpg"))
//                .size(600, 399).watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File(basePath + "/test.jpg")), 0.25f).outputQuality(0.8f)
//                .toFile("/Users/xuwanying/Pictures/test/success.jpg");
//
//    }


    public static String generateThumbnail(MultipartFile multipartFile, String targetAddr) {
        String realFileName = FileUtil.getRandomFileName();
        String extension = getFileExtension(multipartFile);
        makeDirPath(targetAddr);
        String relativeAddr = targetAddr + realFileName + extension;
        File dest = new File(FileUtil.getImgBasePath() + relativeAddr);
        try{
            Thumbnails.of(multipartFile.getInputStream()).size(200,200).outputQuality(0.25f).toFile(dest);
        } catch(IOException e) {
            throw new RuntimeException("创建缩略图失败："+e.getMessage());
        }
        return relativeAddr;
    }

    public static List<String> generateNormalImgs(List<MultipartFile> multipartFileList,String targetAddr){
        int count = 0;
        List<String> relativeAddrList= new ArrayList<>();
        if(multipartFileList!=null&&multipartFileList.size()>0){
            makeDirPath(targetAddr);
            for(MultipartFile multipartFile:multipartFileList){
                String realFileName = FileUtil.getRandomFileName();
                String extension = getFileExtension(multipartFile);
                String relativeAddr = targetAddr+realFileName+extension;
                File dest = new File(FileUtil.getImgBasePath()+relativeAddr);
                count++;
                try{
                    Thumbnails.of(multipartFile.getInputStream()).size(600,300).outputQuality(0.5f).toFile(dest);
                } catch(IOException e) {
                    throw new RuntimeException("创建图片失败："+e.getMessage());
                }
                relativeAddrList.add(relativeAddr);
            }
        }
        return relativeAddrList;
    }



    /**
     * 获取文件后缀名(jpg,..)
     * @param multipartFile
     * @return
     */
    public static String getFileExtension(MultipartFile multipartFile) {
        String originFileName = multipartFile.getOriginalFilename();
        return originFileName.substring(originFileName.lastIndexOf("."));
    }

    public static void makeDirPath(String targetAddr){
        String realFileParentPath =FileUtil.getImgBasePath()+targetAddr; // /User/xuwanying+/upload/images/item/shop/shopid/
        File dirPath = new File(realFileParentPath);
        if(!dirPath.exists()){
            dirPath.mkdirs();
        }
    }



}



